package me.jessyan.mvparms.demo.mvp.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.Image;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.platform.ai.control.R;


/**
 * The author: Zhao X.H.
 * Creation time: 2018/2/14 ${time}
 */

public class VolumePopWindow<T extends String> extends PopupWindow {
    private Context mContext;
    private int currentVolume;
    public VolumePopWindow(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.pop_volume, null);
        TextView mSound = view.findViewById(R.id.tv_sound);
        ImageView mAdd = view.findViewById(R.id.iv_add);
        ImageView mSub = view.findViewById(R.id.iv_sub);
        SeekBar mSeekBar = view.findViewById(R.id.seekbar_def);
//        音量控制,初始化定义
        AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
//        最大音量
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//        当前音量
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume,  AudioManager.FLAG_PLAY_SOUND);
        mSound.setText(String.valueOf(currentVolume));
        mSeekBar.setMax(maxVolume);
        mSeekBar.setProgress(currentVolume);
        //        滑动条部分
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,seekBar.getProgress(),  AudioManager.FLAG_PLAY_SOUND);
                mSound.setText(String.valueOf(seekBar.getProgress()));
            }
        });
        mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentVolume > 10){
                    Toast.makeText(mContext,"音量已最大",Toast.LENGTH_SHORT).show();
                    return;
                }
                currentVolume++;
                mSound.setText(String.valueOf(currentVolume));
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume,  AudioManager.FLAG_PLAY_SOUND);
                mSeekBar.setProgress(currentVolume);
            }
        });
        mSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentVolume < 0){
                    Toast.makeText(mContext,"音量已最小",Toast.LENGTH_SHORT).show();
                    return;
                }
                currentVolume--;
                mSound.setText(String.valueOf(currentVolume));
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume,  AudioManager.FLAG_PLAY_SOUND);
                mSeekBar.setProgress(currentVolume);
            }
        });

        setContentView(view);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);//设置为true的时候才能点击列表中的item
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
        setAnimationStyle(R.style.Popup_Spinner);
//        ColorDrawable dw = new ColorDrawable(-00000);// 半透明
        setBackgroundDrawable(cd);// 设置背景图片，不能在布局中设置，要通过代码来设置
        setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
        setBackgroundAlpha(0.5f);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1.0f);
            }
        });
    }


    public void showPopupWindowByCenter(View v){
//        showAtLocation(v.getRootView(), Gravity.CENTER, 0, 0);//在v的下面
        showAsDropDown(v);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
}
